﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using System.Configuration;
using System.Xml;
using System.Security.Cryptography;
using com.huawei.esdk.tp.oa.Utils;
using com.huawei.esdk.tp.oa.DataType;
using com.huawei.esdk.tp.oa.eSDKTPOAConfMgrService;

namespace com.huawei.esdk.tp.oa.local
{
    public partial class ConferenceMgrService
    {
        private static UtilsSettings appSetting;
        private static string platformKeyURL = string.Empty;

        //RSA2048 rsa;
        private static ConferenceMgrService instance = null;

        private ConferenceMgrService(){ }

        public static ConferenceMgrService Instance(string userName,string password,string serverURL)
        {
            if (null == instance)
            {
                instance = new ConferenceMgrService();
            }

            string[] urlSpilt = serverURL.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            int urlLength = urlSpilt.Length - 1;
            //platformKeyURL = serverURL.Replace(urlSpilt[urlLength], "platformKeyMgr");
            appSetting = new UtilsSettings(userName, password, serverURL, platformKeyURL);
            
            System.Net.HttpWebRequest.DefaultWebProxy = null;
            return instance;
        }

        /// <summary>
        /// 召集或预约非周期性会议
        /// </summary>
        /// <param name="scheduleConf">
        /// 准备预约的会议参数，包含会场列表和参数，预约时 ConferenceInfoEx 必填参数包括：
        /// name：会议名称
        /// string：会议开始时间
        /// rate：速率
        /// duration：会议时长
        /// sites：会场信息列表
        /// 其中会场列表参数中的 SiteInfoEx 必填参数为：
        /// uri 会场标识。当 uri 为空时，预约匿名会议。
        ///</param>
        /// <returns>操作成功则 response.result返回预约成功后会议基本信息，包括系统分配的会议 ID，会议的接入码等信息。</returns>
        public TPOASDKResponse<ConferenceInfo> scheduleConf(ConferenceInfo scheduleConf)
        {
            TPOASDKResponse<ConferenceInfo> resp = new TPOASDKResponse<ConferenceInfo>();
            TPProfessionalConfMgrService confMgr = new TPProfessionalConfMgrService(appSetting);
            //UtilsSettings appSetting = new UtilsSettings();
            confMgr.Header = appSetting.Header;
            try
            {
                //rsa = new RSA2048(platformKeyURL);

                if (null != scheduleConf.auxVideoFormat)
                {
                    scheduleConf.auxVideoFormatSpecified = true;
                }
                if (null != scheduleConf.auxVideoProtocol)
                {
                    scheduleConf.auxVideoProtocolSpecified = true;
                }
                if (null != scheduleConf.isRecording)
                {
                    scheduleConf.isRecordingSpecified = true;
                }
                if (null != scheduleConf.isLiveBroadcast)
                {
                    scheduleConf.isLiveBroadcastSpecified = true;
                }
                if (null != scheduleConf.status)
                {
                    scheduleConf.statusSpecified = true;
                }

                for(int i = 0;i<scheduleConf.sites.Length;i++)
                {
                    if (scheduleConf.sites[i].videoFormat != null)
                    {
                        scheduleConf.sites[i].videoFormatSpecified = true;
                    }
                    if (scheduleConf.sites[i].videoProtocol != null)
                    {
                        scheduleConf.sites[i].videoProtocolSpecified = true;
                    }
                    if (scheduleConf.sites[i].status != null)
                    {
                        scheduleConf.sites[i].statusSpecified = true;
                    }
                }

                //if (scheduleConf.password != null && scheduleConf.password != "")
                //{
                //    scheduleConf.password = rsa.RSAEncrypt(scheduleConf.password);
                //}
                //if (scheduleConf.chairmanPassword != null && scheduleConf.chairmanPassword != "")
                //{
                //    scheduleConf.chairmanPassword = rsa.RSAEncrypt(scheduleConf.chairmanPassword);
                //}
                int resultCode;
                ConferenceInfo conferenceInfoEx;

                resultCode = confMgr.scheduleConf(scheduleConf, out conferenceInfoEx);

                resp.resultCode = resultCode;
                resp.result = 0 == resultCode ? conferenceInfoEx : null;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }
        }

        public TPOASDKResponse<RecurrenceConfInfo> scheduleRecurrenceConference(RecurrenceConfInfo scheduleConf)
        {
            TPOASDKResponse<RecurrenceConfInfo> resp = new TPOASDKResponse<RecurrenceConfInfo>();
            TPProfessionalConfMgrService confMgr = new TPProfessionalConfMgrService(appSetting);
            //UtilsSettings appSetting = new UtilsSettings();
            confMgr.Header = appSetting.Header;
            try
            {
                //rsa = new RSA2048(platformKeyURL);
                if (null != scheduleConf.auxVideoFormat)
                {
                    scheduleConf.auxVideoFormatSpecified = true;
                }
                if (null != scheduleConf.auxVideoProtocol)
                {
                    scheduleConf.auxVideoProtocolSpecified = true;
                }
                if (null != scheduleConf.isRecording)
                {
                    scheduleConf.isRecordingSpecified = true;
                }
                if (null != scheduleConf.isLiveBroadcast)
                {
                    scheduleConf.isLiveBroadcastSpecified = true;
                }

                if (null != scheduleConf.status)
                {
                    scheduleConf.statusSpecified = true;
                }

                for (int i = 0; i < scheduleConf.sites.Length; i++)
                {
                    if (scheduleConf.sites[i].videoFormat != null)
                    {
                        scheduleConf.sites[i].videoFormatSpecified = true;
                    }
                    if (scheduleConf.sites[i].videoProtocol != null)
                    {
                        scheduleConf.sites[i].videoProtocolSpecified = true;
                    }
                    if (scheduleConf.sites[i].status != null)
                    {
                        scheduleConf.sites[i].statusSpecified = true;
                    }
                }

                if (null != scheduleConf.frequency)
                {
                    scheduleConf.frequencySpecified = true;
                }
                
                if (null != scheduleConf.interval)
                {
                    scheduleConf.intervalSpecified = true;
                }

                if(scheduleConf.endDate != null)
                {
                    scheduleConf.endDateSpecified = true;
                }
                //if (scheduleConf.password != null && scheduleConf.password != "")
                //{
                //    scheduleConf.password = rsa.RSAEncrypt(scheduleConf.password);
                //}
                //if (scheduleConf.chairmanPassword != null && scheduleConf.chairmanPassword != "")
                //{
                //    scheduleConf.chairmanPassword = rsa.RSAEncrypt(scheduleConf.chairmanPassword);
                //}
                int resultCode;
                RecurrenceConfInfo conferenceInfoEx;

                resultCode = confMgr.scheduleRecurrenceConference(scheduleConf, out conferenceInfoEx);

                resp.resultCode = resultCode;
                resp.result = 0 == resultCode ? conferenceInfoEx : null;
                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }


        }

        public TPOASDKResponse<ConferenceInfo> editScheduledConf(ConferenceInfo scheduleConf)
        {
            TPOASDKResponse<ConferenceInfo> resp = new TPOASDKResponse<ConferenceInfo>();
            TPProfessionalConfMgrService confMgr = new TPProfessionalConfMgrService(appSetting);
            //UtilsSettings appSetting = new UtilsSettings();
            confMgr.Header = appSetting.Header;
            try
            {
                //rsa = new RSA2048(platformKeyURL);
                if (null != scheduleConf.auxVideoFormat)
                {
                    scheduleConf.auxVideoFormatSpecified = true;
                }
                if (null != scheduleConf.auxVideoProtocol)
                {
                    scheduleConf.auxVideoProtocolSpecified = true;
                }
                if (null != scheduleConf.isRecording)
                {
                    scheduleConf.isRecordingSpecified = true;
                }
                if (null != scheduleConf.isLiveBroadcast)
                {
                    scheduleConf.isLiveBroadcastSpecified = true;
                }

                if (null != scheduleConf.status)
                {
                    scheduleConf.statusSpecified = true;
                }

                for (int i = 0; i < scheduleConf.sites.Length; i++)
                {
                    if (scheduleConf.sites[i].videoFormat != null)
                    {
                        scheduleConf.sites[i].videoFormatSpecified = true;
                    }
                    if (scheduleConf.sites[i].videoProtocol != null)
                    {
                        scheduleConf.sites[i].videoProtocolSpecified = true;
                    }
                    if (scheduleConf.sites[i].status != null)
                    {
                        scheduleConf.sites[i].statusSpecified = true;
                    }
                }

                //if (scheduleConf.password != null && scheduleConf.password != "")
                //{
                //    scheduleConf.password = rsa.RSAEncrypt(scheduleConf.password);
                //}
                //if (scheduleConf.chairmanPassword != null && scheduleConf.chairmanPassword != "")
                //{
                //    scheduleConf.chairmanPassword = rsa.RSAEncrypt(scheduleConf.chairmanPassword);
                //}
                int resultCode;
                ConferenceInfo conferenceInfoEx;

                resultCode = confMgr.editScheduledConf(scheduleConf, out conferenceInfoEx);

                resp.resultCode = resultCode;
                resp.result = 0 == resultCode ? conferenceInfoEx : null;

                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public TPOASDKResponse<RecurrenceConfInfo> editRecurrenceConference(RecurrenceConfInfo scheduleConf, System.Nullable<System.DateTime> beginTime)
        {
            TPOASDKResponse<RecurrenceConfInfo> resp = new TPOASDKResponse<RecurrenceConfInfo>();
            TPProfessionalConfMgrService confMgr = new TPProfessionalConfMgrService(appSetting);
            //UtilsSettings appSetting = new UtilsSettings();
            confMgr.Header = appSetting.Header;
            try
            {
                //rsa = new RSA2048(platformKeyURL);
                if (null != scheduleConf.auxVideoFormat)
                {
                    scheduleConf.auxVideoFormatSpecified = true;
                }
                if (null != scheduleConf.auxVideoProtocol)
                {
                    scheduleConf.auxVideoProtocolSpecified = true;
                }
                if (null != scheduleConf.isRecording)
                {
                    scheduleConf.isRecordingSpecified = true;
                }
                if (null != scheduleConf.isLiveBroadcast)
                {
                    scheduleConf.isLiveBroadcastSpecified = true;
                }

                if (null != scheduleConf.status)
                {
                    scheduleConf.statusSpecified = true;
                }

                for (int i = 0; i < scheduleConf.sites.Length; i++)
                {
                    if (scheduleConf.sites[i].videoFormat != null)
                    {
                        scheduleConf.sites[i].videoFormatSpecified = true;
                    }
                    if (scheduleConf.sites[i].videoProtocol != null)
                    {
                        scheduleConf.sites[i].videoProtocolSpecified = true;
                    }
                    if (scheduleConf.sites[i].status != null)
                    {
                        scheduleConf.sites[i].statusSpecified = true;
                    }
                }

                if (null != scheduleConf.frequency)
                {
                    scheduleConf.frequencySpecified = true;
                }

                if (null != scheduleConf.interval)
                {
                    scheduleConf.intervalSpecified = true;
                }

                if (scheduleConf.endDate != null)
                {
                    scheduleConf.endDateSpecified = true;
                }
                //if (scheduleConf.password != null && scheduleConf.password != "")
                //{
                //    scheduleConf.password = rsa.RSAEncrypt(scheduleConf.password);
                //}
                //if (scheduleConf.chairmanPassword != null && scheduleConf.chairmanPassword != "")
                //{
                //    scheduleConf.chairmanPassword = rsa.RSAEncrypt(scheduleConf.chairmanPassword);
                //}
                int resultCode;
                RecurrenceConfInfo conferenceInfoEx;
                bool beginTimeSpecified = false;
                if (null != beginTime)
                {
                    beginTimeSpecified = true;
                }
                resultCode = confMgr.editRecurrenceConference(scheduleConf, beginTime, beginTimeSpecified, out conferenceInfoEx);

                resp.resultCode = resultCode;
                resp.result = 0 == resultCode ? conferenceInfoEx : null;

                return resp;
            }
            catch (SoapHeaderException e)
            {
                int errorCode = ExceptionUtils.processSoapException(e);
                resp.resultCode = errorCode;
                return resp;
            }

        }

        public int delScheduledConf(string confId, System.Nullable< System.DateTime> beginTime)
        {
            TPProfessionalConfMgrService confMgr = new TPProfessionalConfMgrService(appSetting);
            //UtilsSettings appSetting = new UtilsSettings();
            confMgr.Header = appSetting.Header;
            int resultCode;
            try
            {
                bool beginDateSpecified = false;
                if (null != beginTime)
                {
                    beginDateSpecified = true;
                }

                resultCode = confMgr.delScheduledConf(confId, beginTime, beginDateSpecified);
                return resultCode;
            }
            catch (SoapHeaderException e)
            {
                return ExceptionUtils.processSoapException(e);
            }

        }                 
    }
}
